package com.ts.mapper.achievement;

import com.ts.entity.Patent;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PatentMapper {

    /**
     * 新增专利（主键自增返回）
     * @param patent 专利实体
     */
    @Insert("INSERT INTO `tianshi-college-teacher-system-test`.ts_patent " +
            "(achievement_type, is_first_author, is_first_in_unit, patent_name, author, " +
            "`rank`, is_teacher, is_student, authorization_number, patent_type, " +  // 反引号包裹关键字`rank`
            "approval_time, subject_category, achievement_source_name, achievement_source_level, " +
            "is_applied, is_industry_joint, is_first_signing_unit, remark, evidence_material, " +
            "create_by, create_time, update_by, update_time) " +
            "VALUES " +
            "(#{achievementType}, #{isFirstAuthor}, #{isFirstInUnit}, #{patentName}, #{author}, " +
            "#{rank}, #{isTeacher}, #{isStudent}, #{authorizationNumber}, #{patentType}, " +
            "#{approvalTime}, #{subjectCategory}, #{achievementSourceName}, #{achievementSourceLevel}, " +
            "#{isApplied}, #{isIndustryJoint}, #{isFirstSigningUnit}, #{remark}, #{evidenceMaterial}, " +
            "#{createBy}, #{createTime}, #{updateBy}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")  // 自增ID回写
    void insert(Patent patent);

    /**
     * 根据ID删除专利
     * @param id 专利ID
     * @return 影响行数
     */
    @Delete("DELETE FROM `tianshi-college-teacher-system-test`.ts_patent WHERE id = #{id}")
    int deleteById(Long id);

    /**
     * 根据ID更新专利（全字段更新）
     * @param patent 专利实体（需包含ID）
     * @return 影响行数
     */
    @Update("UPDATE `tianshi-college-teacher-system-test`.ts_patent SET " +
            "achievement_type = #{achievementType}, " +
            "is_first_author = #{isFirstAuthor}, " +
            "is_first_in_unit = #{isFirstInUnit}, " +
            "patent_name = #{patentName}, " +
            "author = #{author}, " +
            "`rank` = #{rank}, " +  // 反引号包裹关键字`rank`
            "is_teacher = #{isTeacher}, " +
            "is_student = #{isStudent}, " +
            "authorization_number = #{authorizationNumber}, " +
            "patent_type = #{patentType}, " +
            "approval_time = #{approvalTime}, " +
            "subject_category = #{subjectCategory}, " +
            "achievement_source_name = #{achievementSourceName}, " +
            "achievement_source_level = #{achievementSourceLevel}, " +
            "is_applied = #{isApplied}, " +
            "is_industry_joint = #{isIndustryJoint}, " +
            "is_first_signing_unit = #{isFirstSigningUnit}, " +
            "remark = #{remark}, " +
            "evidence_material = #{evidenceMaterial}, " +
            "create_by = #{createBy}, " +  // 保留创建人（如需禁止修改可删除此行）
            "create_time = #{createTime}, " +  // 保留创建时间（如需禁止修改可删除此行）
            "update_by = #{updateBy}, " +
            "update_time = #{updateTime} " +
            "WHERE id = #{id}")
    int updateById(Patent patent);

    /**
     * 根据ID查询专利
     * @param id 专利ID
     * @return 专利实体
     */
    @Select("SELECT * FROM `tianshi-college-teacher-system-test`.ts_patent WHERE id = #{id}")
    Patent selectById(Long id);

    /**
     * 查询所有专利
     * @return 专利列表
     */
    @Select("SELECT * FROM `tianshi-college-teacher-system-test`.ts_patent")
    List<Patent> selectAll();

    /**
     * 分页查询专利
     * @param offset 偏移量（(pageNum-1)*pageSize）
     * @param limit 每页条数
     * @return 分页专利列表
     */
    @Select("SELECT * FROM `tianshi-college-teacher-system-test`.ts_patent LIMIT #{offset}, #{limit}")
    List<Patent> selectByPage(@Param("offset") Integer offset, @Param("limit") Integer limit);

    /**
     * 查询专利总条数（用于分页）
     * @return 总记录数
     */
    @Select("SELECT COUNT(*) FROM `tianshi-college-teacher-system-test`.ts_patent")
    Long selectTotalCount();
}